package com.admin.system.aspect;

import javax.servlet.http.HttpServletRequest;

import com.admin.system.entity.SystemResource;
import com.admin.system.service.SystemResourceService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

/**
 * 查询展示页面标签名称
 *
 * @author : alex
 * @date : 2020/6/7
 */
@Component
@Aspect
public class TitleAspect {
    @Autowired
    SystemResourceService resourceService;

    @After("execution(* com.admin..*.controller.*.list(..))")
    public void injectTitle(JoinPoint joinPoint) {
        String title = "";
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof HttpServletRequest) {
                HttpServletRequest request = (HttpServletRequest) arg;
                String url = request.getServletPath();
                //查询url，来获取当前页面标签名称
                SystemResource resource = resourceService.queryResourceByUrl(url);
                title = resource.getNameZh();
            }
        }
        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof ModelAndView) {
                ModelAndView mav = (ModelAndView) arg;
                mav.addObject("title", title);
            }
        }

    }
}
